<template>
  <div class="seller-container">
    <div class="sellerchart" ref="seller_ref"></div>
    <span class="iconfont icon-arrow-lift" @click="handleToLeft"></span>
    <span class="iconfont icon-arrow-right" @click="handleToright"></span>
    <span class="cate-name">{{ cateName }}</span>
  </div>
</template>

<script>
export default {
  name: "Hot",
  data() {
    return {
      chartInstance: null,
      allData: "",
      currentIndex: 0,
      timerId: "",
    };
  },
  computed: {
    cateName() {
      if (!this.allData) {
        return "";
      }
      return this.allData[this.currentIndex].name;
    },
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echart.init(this.$refs.seller_ref, "customed");

      const initOption = {
        title: {
          text: "| 热销商品占比",
          top: 20,
          left: 20,
        },
        legend: {
          top: "5%",
          icon: "circle",
        },
        tooltip: {
          show: true,
          formatter: (arg) => {
            const thirdcate = arg.data.children;
            let total = 0;
            thirdcate.forEach((item) => {
              total += item.value;
            });
            let retStr = "";
            thirdcate.forEach((item) => {
              retStr += `
                ${item.name}:${parseInt(item.value/total*100)+ '%' }
                <br />
              `
            });
            return retStr
          },
        },
        series: [
          {
            type: "pie",
            label: {
              show: false,
              position: "outside",
              textStyle: {
                color: "#fff",
              },
            },
            emphasis: {
              label: {
                show: true,
              },
              labelLine: {
                show: false,
              },
            },
          },
        ],
      };
      initOption && this.chartInstance.setOption(initOption);
    },
    async getData() {
      const { data: ret } = await this.$http.get("hotproduct");
      this.allData = ret;

      this.updateChart();
    },
    updateChart() {
      const seriesData = this.allData[this.currentIndex].children.map(
        (item) => {
          return {
            name: item.name,
            value: item.value,
            children: item.children,
          };
        }
      );
      const legendData = this.allData[this.currentIndex].children.map(
        (item) => {
          return item.name;
        }
      );

      const dataOption = {
        legend: {
          data: legendData,
        },
        series: [
          {
            data: seriesData,
          },
        ],
      };
      dataOption && this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const title = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: title,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: title,
            },
          },
        },
        series: [
          {
            itemStyle: {
              borderRadius: [0, 0, 0, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    handleToLeft() {
      this.currentIndex--;
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1;
      }
      this.updateChart();
    },
    handleToright() {
      this.currentIndex++;
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0;
      }
      this.updateChart();
    },
  },
};
</script>

<style lang="scss" scoped>
.iconfont {
  position: absolute;
  transform: translateY(-50%);
  top: 50%;
  cursor: pointer;
  font-size: 60px;
  color: #fff;
}
.icon-arrow-lift {
  left: 10%;
}
.icon-arrow-right {
  right: 10%;
}
.cate-name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: #fff;
}
</style>